package com.example.tao_city.text1;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

/**
 * Created by TAO_CITY on 2018/1/22.
 */

public class Nfcchongzhi extends Activity implements View.OnClickListener{
    private ImageView back,weixin,yinglain,zhifubao,wode;
    private TextView jiage10,jiage20,jiage30,jiage50,jiage100,jiage200;
    private Button button;
    private String qian="";
    private TextView kahao,yue;
    private PopupWindow mpopuwindow;
    private ImageView pwback,pw_tuige,kong;
    private ImageView mima1,mima2,mima3,mima4,mima5,mima6;
    private TextView pw_0,pw_1,pw_2,pw_3,pw_4,pw_5,pw_6,pw_7,pw_8,pw_9,jine;
    private int mima=1;
    private String password="";
    private WindowManager.LayoutParams lp;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.nfc_fukuang);
        ininview();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        finish();
    }

    private void ininview() {
        back=findViewById(R.id.nfcfukuang_back);
        jiage10=findViewById(R.id.nfcfukuang10);
        jiage20=findViewById(R.id.nfcfukuang20);
        jiage30=findViewById(R.id.nfcfukuang30);
        jiage50=findViewById(R.id.nfcfukuang50);
        jiage100=findViewById(R.id.nfcfukuang100);
        jiage200=findViewById(R.id.nfcfukuang200);
        weixin=findViewById(R.id.nfcfukuang_weixin);
        yinglain=findViewById(R.id.nfcfukuang_yinglian);
        zhifubao=findViewById(R.id.nfcfukuang_zhifubao);
        wode=findViewById(R.id.nfcfukuang_zhanghu);
        button=findViewById(R.id.nfcfukuang_button);
        kahao=findViewById(R.id.nfcfukuang_number);
        yue=findViewById(R.id.nfcfukuang_yue);

        button.setOnClickListener(this);
        wode.setOnClickListener(this);
        weixin.setOnClickListener(this);
        yinglain.setOnClickListener(this);
        zhifubao.setOnClickListener(this);
        jiage10.setOnClickListener(this);
        jiage20.setOnClickListener(this);
        jiage30.setOnClickListener(this);
        jiage50.setOnClickListener(this);
        jiage100.setOnClickListener(this);
        jiage200.setOnClickListener(this);
        back.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.nfcfukuang_back:
                finish();
                break;
            case R.id.nfcfukuang10:
                setjiagebackground();
                qian="10";
                Toast.makeText(this,"选择了"+qian,Toast.LENGTH_SHORT).show();
                jiage10.setBackgroundResource(R.drawable.recharge_jiageoncli);
                jiage10.setTextColor(Color.parseColor("#458fee"));
                break;
            case R.id.nfcfukuang20:
                setjiagebackground();
                qian="20";
                Toast.makeText(this,"选择了"+qian,Toast.LENGTH_SHORT).show();
                jiage20.setBackgroundResource(R.drawable.recharge_jiageoncli);
                jiage20.setTextColor(Color.parseColor("#458fee"));
                break;
            case R.id.nfcfukuang30:
                setjiagebackground();
                qian="30";
                Toast.makeText(this,"选择了"+qian,Toast.LENGTH_SHORT).show();
                jiage30.setBackgroundResource(R.drawable.recharge_jiageoncli);
                jiage30.setTextColor(Color.parseColor("#458fee"));
                break;
            case R.id.nfcfukuang50:
                setjiagebackground();
                qian="50";
                Toast.makeText(this,"选择了"+qian,Toast.LENGTH_SHORT).show();
                jiage50.setBackgroundResource(R.drawable.recharge_jiageoncli);
                jiage50.setTextColor(Color.parseColor("#458fee"));
                break;
            case R.id.nfcfukuang100:
                setjiagebackground();
                qian="100";
                Toast.makeText(this,"选择了"+qian,Toast.LENGTH_SHORT).show();
                jiage100.setBackgroundResource(R.drawable.recharge_jiageoncli);
                jiage100.setTextColor(Color.parseColor("#458fee"));
                break;
            case R.id.nfcfukuang200:
                setjiagebackground();
                qian="200";
                Toast.makeText(this,"选择了"+qian,Toast.LENGTH_SHORT).show();
                jiage200.setBackgroundResource(R.drawable.recharge_jiageoncli);
                jiage200.setTextColor(Color.parseColor("#458fee"));
                break;
            case R.id.nfcfukuang_weixin:
                setzhifuimg();
                weixin.setImageResource(R.drawable.icon_weixin1);
                break;
            case R.id.nfcfukuang_yinglian:
                setzhifuimg();
                yinglain.setImageResource(R.drawable.icon_yinglian1);
                break;
            case R.id.nfcfukuang_zhifubao:
                setzhifuimg();
                zhifubao.setImageResource(R.drawable.icon_zhifubao1);
                break;
            case R.id.nfcfukuang_zhanghu:
                setzhifuimg();
                wode.setImageResource(R.drawable.icon_wode1);
                break;
            case R.id.nfcfukuang_button:
                if(qian==""){
                    Toast.makeText(Nfcchongzhi.this,"充值为空，请重新选择",Toast.LENGTH_SHORT).show();
                }else {
                    showmima();
                    lp=getWindow().getAttributes();
                    lp.alpha=0.6f;
                    getWindow().setAttributes(lp);
                    getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);

                   /* Intent in = new Intent();
                    String k= String.valueOf(kahao.getText());
                    String y= String.valueOf(yue.getText());
                    in.putExtra("kahao",k);
                    in.putExtra("yue",y);
                    in.putExtra("qian", qian);
                    in.setClass(Nfcchongzhi.this, nfcchongzhizhong.class);
                    startActivity(in);
                    break;*/
                }
                break;
            case R.id.nfc_pw_back:
                mpopuwindow.dismiss();
                break;
            case R.id.nfc_pw_0:
                addpassword(0);
                setmima(mima);
                break;
            case R.id.nfc_pw_1:
                addpassword(1);
                setmima(mima);
                break;
            case R.id.nfc_pw_2:
                addpassword(2);
                setmima(mima);
                break;
            case R.id.nfc_pw_3:
                addpassword(3);
                setmima(mima);
                break;
            case R.id.nfc_pw_4:
                addpassword(4);
                setmima(mima);
                break;
            case R.id.nfc_pw_5:
                addpassword(5);
                setmima(mima);
                break;
            case R.id.nfc_pw_6:
                addpassword(6);
                setmima(mima);
                break;
            case R.id.nfc_pw_7:
                addpassword(7);
                setmima(mima);
                break;
            case R.id.nfc_pw_8:
                addpassword(8);
                setmima(mima);
                break;
            case R.id.nfc_pw_9:
                addpassword(9);
                setmima(mima);
                break;
            case R.id.nfc_pw_tuige:
                settuige(mima);
                if(!password.equals("")) {
                    password = password.substring(0, password.length() - 1);
                }
                break;
            case R.id.nfc_pw_kong:
                Toast.makeText(this,password,Toast.LENGTH_SHORT).show();
                break;
        }
    }
    private void setjiagebackground(){
        jiage10.setBackgroundResource(R.drawable.recharge_jiage);
        jiage20.setBackgroundResource(R.drawable.recharge_jiage);
        jiage30.setBackgroundResource(R.drawable.recharge_jiage);
        jiage50.setBackgroundResource(R.drawable.recharge_jiage);
        jiage100.setBackgroundResource(R.drawable.recharge_jiage);
        jiage200.setBackgroundResource(R.drawable.recharge_jiage);

        jiage10.setTextColor(Color.parseColor("#d9d9da"));
        jiage20.setTextColor(Color.parseColor("#d9d9da"));
        jiage30.setTextColor(Color.parseColor("#d9d9da"));
        jiage50.setTextColor(Color.parseColor("#d9d9da"));
        jiage100.setTextColor(Color.parseColor("#d9d9da"));
        jiage200.setTextColor(Color.parseColor("#d9d9da"));
    }
    private void setzhifuimg(){
        weixin.setImageResource(R.drawable.icon_weixin);
        zhifubao.setImageResource(R.drawable.icon_zhifubao);
        yinglain.setImageResource(R.drawable.icon_yinglian);
        wode.setImageResource(R.drawable.icon_wode);
    }

    private void showmima(){
        View view=getLayoutInflater().inflate(R.layout.nfc_password,null);
        jine=view.findViewById(R.id.nfc_pw_jine);
        jine.setText("充值金额"+qian+"元");
        mpopuwindow=new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        mpopuwindow.setFocusable(true);//需要设置为ture，表示可以聚焦　　　　　　　　
        mpopuwindow.setBackgroundDrawable(new BitmapDrawable(getResources()));
        View rootview = LayoutInflater.from(Nfcchongzhi.this).inflate(R.layout.nfc_fukuang,null);
        mpopuwindow.showAtLocation(rootview, Gravity.BOTTOM,0,0);//显示在主页面（rootview）的底部
        mpopuwindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                mima=1;
                setmimakong();
                password="";
                lp.alpha=1f;
                getWindow().setAttributes(lp);
                getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
            }
        });

        mima1=view.findViewById(R.id.nfc_pw_mima1);
        mima2=view.findViewById(R.id.nfc_pw_mima2);
        mima3=view.findViewById(R.id.nfc_pw_mima3);
        mima4=view.findViewById(R.id.nfc_pw_mima4);
        mima5=view.findViewById(R.id.nfc_pw_mima5);
        mima6=view.findViewById(R.id.nfc_pw_mima6);
        pw_0=view.findViewById(R.id.nfc_pw_0);
        pw_1=view.findViewById(R.id.nfc_pw_1);
        pw_2=view.findViewById(R.id.nfc_pw_2);
        pw_3=view.findViewById(R.id.nfc_pw_3);
        pw_4=view.findViewById(R.id.nfc_pw_4);
        pw_5=view.findViewById(R.id.nfc_pw_5);
        pw_6=view.findViewById(R.id.nfc_pw_6);
        pw_7=view.findViewById(R.id.nfc_pw_7);
        pw_8=view.findViewById(R.id.nfc_pw_8);
        pw_9=view.findViewById(R.id.nfc_pw_9);
        pw_tuige=view.findViewById(R.id.nfc_pw_tuige);
        pwback=view.findViewById(R.id.nfc_pw_back);
        kong=view.findViewById(R.id.nfc_pw_kong);

        kong.setOnClickListener(this);
        pwback.setOnClickListener(this);
        pw_0.setOnClickListener(this);
        pw_1.setOnClickListener(this);
        pw_2.setOnClickListener(this);
        pw_3.setOnClickListener(this);
        pw_4.setOnClickListener(this);
        pw_5.setOnClickListener(this);
        pw_6.setOnClickListener(this);
        pw_7.setOnClickListener(this);
        pw_8.setOnClickListener(this);
        pw_9.setOnClickListener(this);
        pw_tuige.setOnClickListener(this);

    }
    private void setmima(int i){
        switch (i){
            case 1:
                mima1.setImageResource(R.drawable.heidian1);
                mima++;
                break;
            case 2:
                mima2.setImageResource(R.drawable.heidian1);
                mima++;
                break;
            case 3:
                mima3.setImageResource(R.drawable.heidian1);
                mima++;
                break;
            case 4:
                mima4.setImageResource(R.drawable.heidian1);
                mima++;
                break;
            case 5:
                mima5.setImageResource(R.drawable.heidian1);
                mima++;
                break;
            case 6:
                mima6.setImageResource(R.drawable.heidian1);
                mima++;
                if(password.equals("112306")) {
                    Intent in = new Intent();
                    String k = String.valueOf(kahao.getText());
                    String y = String.valueOf(yue.getText());
                    in.putExtra("kahao", k);
                    in.putExtra("yue", y);
                    in.putExtra("qian", qian);
                    in.setClass(Nfcchongzhi.this, nfcchongzhizhong.class);
                    startActivity(in);
                }else {
                    Toast.makeText(this,"密码错误，请重输",Toast.LENGTH_SHORT).show();
                    password="";
                    setmimakong();
                    mima=1;
                }
                break;
        }
    }
    private void setmimakong(){
        mima1.setImageResource(R.drawable.heidian1_kong);
        mima2.setImageResource(R.drawable.heidian1_kong);
        mima3.setImageResource(R.drawable.heidian1_kong);
        mima4.setImageResource(R.drawable.heidian1_kong);
        mima5.setImageResource(R.drawable.heidian1_kong);
        mima6.setImageResource(R.drawable.heidian1_kong);
    }
    private void settuige(int i){
        switch (i){
            case 2:
                mima1.setImageResource(R.drawable.heidian1_kong);
                mima--;
                break;
            case 3:
                mima2.setImageResource(R.drawable.heidian1_kong);
                mima--;
                break;
            case 4:
                mima3.setImageResource(R.drawable.heidian1_kong);
                mima--;
                break;
            case 5:
                mima4.setImageResource(R.drawable.heidian1_kong);
                mima--;
                break;
            case 6:
                mima5.setImageResource(R.drawable.heidian1_kong);
                mima--;
                break;
            case 7:
                mima6.setImageResource(R.drawable.heidian1_kong);
                mima--;
                break;
        }
    }
    private void addpassword(int p){
        if (password.length()<6){
            password=password+p;
        }
    }
}
